package br.com.prezz.fileupload.client;

import com.google.gwt.core.client.EntryPoint;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import br.com.prezz.client.LoginInfo;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class FileUpload implements EntryPoint {
	@Override
	public void onModuleLoad() {
		LoginInfo loginInfo = new LoginInfo();
		if (loginInfo.isLoggedIn() && loginInfo.isAdmin()) {
			RootPanel rootPanel = RootPanel.get();
			
			VerticalPanel verticalPanel = new VerticalPanel();
			verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			rootPanel.add(verticalPanel, 20, 10);
			verticalPanel.setSize("60%", "100px");
			
			FlexTable flexTable = new FlexTable();
			verticalPanel.add(flexTable);
			flexTable.setSize("100%", "100%");
			
			final FormPanel formPanel = new FormPanel();
			formPanel.addSubmitHandler(new SubmitHandler() {
				public void onSubmit(SubmitEvent event) {
					Window.alert("Enviado com sucesso.");
					// TODO: Processar XML
				}
			});
			flexTable.setWidget(0, 0, formPanel);
			
			com.google.gwt.user.client.ui.FileUpload fileUpload = new com.google.gwt.user.client.ui.FileUpload();
			formPanel.setWidget(fileUpload);
			fileUpload.setSize("100%", "100%");
			
			Button btnEnviar = new Button("Enviar");
			btnEnviar.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					formPanel.submit();
				}
			});
			flexTable.setWidget(1, 0, btnEnviar);
		} else {
			Window.alert("Acesso Negado");
		}
	}
}
